package com.seo.service.report.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seo.mapper.bill.BillMapper;
import com.seo.mapper.bill.proxy.ProxyTransactionMapper;
import com.seo.mapper.proxy.recharge.ProxyRechargeMapper;
import com.seo.mapper.user.UserMapper;
import com.seo.mapper.user.recharge.RechargeMapper;
import com.seo.pojo.ReturnBody;
import com.seo.service.report.sdk.ReportService;
import com.seo.utils.DateUtil;
import com.seo.utils.Jurisdiction;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("reportService")
public class ReportServiceImpl implements ReportService {

	@Autowired
	private BillMapper billMapper;

	@Autowired
	private ProxyTransactionMapper proxyTransactionMapper;

	@Autowired
	private RechargeMapper rechargeMapper;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private ProxyRechargeMapper proxyRechargeMapper;

	@Override
	public ReturnBody getReportIndex() {
		log.info("查询首页报表");
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> params = new HashMap<>();
		String startTime = DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -29, 1);
		String endTime = DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), 1, 1);
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		Map<String, Map<String, Object>> deduc = billMapper.getReportDeducDay(params);
		Map<String, Map<String, Object>> recharge = rechargeMapper.getReportRechargeDay(params);
		List<String> xAxis = new ArrayList<>();
		List<Object> deducList = new ArrayList<>();
		List<Object> rechargeList = new ArrayList<>();
		for (int i = -29; i <= 0; i++) {
			String time = DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), i, 2);
			xAxis.add(time);
			if (deduc.containsKey(time)) {
				deducList.add(deduc.get(time).get("amount"));
			} else {
				deducList.add(0);
			}
			if (recharge.containsKey(time)) {
				rechargeList.add(recharge.get(time).get("amount"));
			} else {
				rechargeList.add(0);
			}
		}

		rb.put("xAxis", xAxis);
		rb.put("deducList", deducList);
		rb.put("rechargeList", rechargeList);
		return rb.success();
	}

	@Override
	public ReturnBody getReportNewIndex(Map<String, Object> params) {
		log.info("查询首页报表");
		ReturnBody rb = ReturnBody.init();
		Map<String, Map<String, Object>> resultList = new LinkedHashMap<>();
		List<Map<String, Object>> resultEList = new ArrayList<>();
		Object belongId = params.get("belongId");

		log.info("今日预计消费");
		params.put("parentId", 0);
		params.put("runTime", DateUtil.getTimesmorning());
		BigDecimal todayAmount = billMapper.getReportExpectConsumeSum(params);
		BigDecimal todayNewAmount = billMapper.getReportNewExpectConsumeSum(params);
		Map<String, Object> todayMap = new HashMap<>();
		todayMap.put("name", "今日预计消费");
		todayMap.put("value", todayAmount.add(todayNewAmount));
		todayMap.put("name_k", "优化");
		todayMap.put("value_k", todayAmount);
		todayMap.put("name_new", "新站");
		todayMap.put("value_new", todayNewAmount);
		todayMap.put("unit", "元");
		resultList.put("todayMap", todayMap);

		log.info("昨日消费金额");
		params.put("parentId", 0);
		params.put("startTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1));
		params.put("endTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1));
		BigDecimal yesterdayAmount = billMapper.getReportDeducSum(params);
		BigDecimal yesterdayNewAmount = billMapper.getReportNewDeducSum(params);
		Map<String, Object> yesterdayMap = new HashMap<>();
		yesterdayMap.put("name", "昨日消费");
		yesterdayMap.put("value", yesterdayAmount.add(yesterdayNewAmount));
		yesterdayMap.put("name_k", "优化");
		yesterdayMap.put("value_k", yesterdayAmount);
		yesterdayMap.put("name_new", "新站");
		yesterdayMap.put("value_new", yesterdayNewAmount);
		yesterdayMap.put("unit", "元");
		resultList.put("yesterdayMap", yesterdayMap);

		log.info("近30天消费金额");
		params.put("parentId", 0);
		params.put("startTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -30, 1));
		params.put("endTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1));
		BigDecimal last30Amount = billMapper.getReportDeducSum(params);
		BigDecimal last30NewAmount = billMapper.getReportNewDeducSum(params);
		Map<String, Object> last30Map = new HashMap<>();
		last30Map.put("name", "近30天消费");
		last30Map.put("value", last30Amount.add(last30NewAmount));
		last30Map.put("name_k", "优化");
		last30Map.put("value_k", last30Amount);
		last30Map.put("name_new", "新站");
		last30Map.put("value_new", last30NewAmount);
		last30Map.put("unit", "元");
		resultList.put("last30Map", last30Map);

		log.info("上个月消费金额");
		params.put("parentId", 0);
		params.put("startTime", DateUtil.getMoneFistDay());
		params.put("endTime", DateUtil.getMoneLastDay());
		BigDecimal lastMonthAmount = billMapper.getReportDeducSum(params);
		BigDecimal lastMonthNewAmount = billMapper.getReportNewDeducSum(params);
		Map<String, Object> lastMonthMap = new HashMap<>();
		lastMonthMap.put("name", "上月消费");
		lastMonthMap.put("value", lastMonthAmount.add(lastMonthNewAmount));
		lastMonthMap.put("name_k", "优化");
		lastMonthMap.put("value_k", lastMonthAmount);
		lastMonthMap.put("name_new", "新站");
		lastMonthMap.put("value_new", lastMonthNewAmount);
		lastMonthMap.put("unit", "元");
		resultList.put("lastMonthMap", lastMonthMap);

		log.info("总消费金额");
		params.clear();
		params.put("parentId", 0);
		params.put("belongId", belongId);
		BigDecimal allAmount = billMapper.getReportDeducSum(params);
		BigDecimal allNewAmount = billMapper.getReportNewDeducSum(params);
		Map<String, Object> allMap = new HashMap<>();
		allMap.put("name", "总消费");
		allMap.put("value", allAmount.add(allNewAmount));
		allMap.put("name_k", "优化");
		allMap.put("value_k", allAmount);
		allMap.put("name_new", "新站");
		allMap.put("value_new", allNewAmount);
		allMap.put("unit", "元");
		resultList.put("allMap", allMap);

		params.clear();
		params.put("belongId", belongId);
		params.put("creationDate", DateUtil.getTimesmorning());
		params.put("parentId", 0);
		int addUserCnt = userMapper.getUserCnt(params);
		Map<String, Object> addUserCntMap = new HashMap<>();
		addUserCntMap.put("name", "今日新增客户");
		addUserCntMap.put("value", addUserCnt);
		addUserCntMap.put("unit", "人");
		resultList.put("addUserCntMap", addUserCntMap);

		params.clear();
		params.put("parentId", 0);
		params.put("belongId", belongId);
		params.put("startTime", DateUtil.getTimesmorning());
		params.put("endTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), 1, 1));
		BigDecimal addRecharge = rechargeMapper.getReportRechargeSum(params);
		Map<String, Object> addRechargeMap = new HashMap<>();
		addRechargeMap.put("name", "今日充值");
		addRechargeMap.put("value", addRecharge);
		addRechargeMap.put("unit", "元");
		resultList.put("addRechargeMap", addRechargeMap);

		params.clear();
		params.put("belongId", belongId);
		params.put("runTime", DateUtil.getTimesmorning());

		List<Map<String, Object>> engineList = billMapper.getEngineList();

		if (engineList != null && engineList.size() > 0) {
			Map<String, Map<String, Object>> engineStatistics = billMapper.getEngineStatistics(params);
			Map<String, Map<String, Object>> engineStatisticsNew = billMapper.getNewEngineStatistics(params);
			for (Map<String, Object> engine : engineList) {
				Map<String, Object> resultObject = new LinkedHashMap<>();
				String engineCode = (String) engine.get("code");
				Map<String, Object> obj = engineStatistics.get(engineCode);
				Map<String, Object> objNew = engineStatisticsNew.get(engineCode);
				if (obj == null && objNew == null) {
					continue;
				}
				String engineName = (String) engine.get("name");
				Integer allNum = 0, allNumK = 0, allNumNew = 0;
				Integer targetNum = 0, targetNumK = 0, targetNumNew = 0;
				Integer newTargetNum = 0, newTargetNumK = 0, newTargetNumNew = 0;
				Integer noTargetNum = 0, noTargetNumK = 0, noTargetNumNew = 0;
				BigDecimal expectConsume = BigDecimal.ZERO, expectConsumeK = BigDecimal.ZERO,
						expectConsumeNew = BigDecimal.ZERO;
				BigDecimal targetRate = BigDecimal.ZERO, targetRateK = BigDecimal.ZERO, targetRateNew = BigDecimal.ZERO;
				if (obj != null) {
					allNumK = (Integer) obj.get("allNum");
					targetNumK = (Integer) obj.get("targetNum");
					newTargetNumK = (Integer) obj.get("newTargetNum");
					noTargetNumK = (Integer) obj.get("noTargetNum");
					expectConsumeK = (BigDecimal) obj.get("expectConsume");
					allNum += allNumK;
					targetNum += targetNumK;
					newTargetNum += newTargetNumK;
					noTargetNum += noTargetNumK;
					expectConsume = expectConsume.add(expectConsumeK);
				}
				if (objNew != null) {
					allNumNew = (Integer) objNew.get("allNum");
					targetNumNew = (Integer) objNew.get("targetNum");
					newTargetNumNew = (Integer) objNew.get("newTargetNum");
					noTargetNumNew = (Integer) objNew.get("noTargetNum");
					expectConsumeNew = (BigDecimal) objNew.get("expectConsume");
					allNum += allNumNew;
					targetNum += targetNumNew;
					newTargetNum += newTargetNumNew;
					noTargetNum += noTargetNumNew;
					expectConsume = expectConsume.add(expectConsumeNew);
				}
				if (allNum > 0) {
					targetRate = new BigDecimal(targetNum).divide(new BigDecimal(allNum), 4, BigDecimal.ROUND_HALF_UP)
							.multiply(new BigDecimal(100)).setScale(2);
				}
				if (allNumK > 0) {
					targetRateK = new BigDecimal(targetNumK)
							.divide(new BigDecimal(allNumK), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))
							.setScale(2);
				}
				if (allNumNew > 0) {
					targetRateNew = new BigDecimal(targetNumNew)
							.divide(new BigDecimal(allNumNew), 4, BigDecimal.ROUND_HALF_UP)
							.multiply(new BigDecimal(100)).setScale(2);
				}

				Map<String, Object> engineAllMap = new HashMap<>();
				engineAllMap.put("name", engineName + "词总数");
				engineAllMap.put("value", allNum);
				engineAllMap.put("name_k", "优化");
				engineAllMap.put("value_k", allNumK);
				engineAllMap.put("name_new", "新站");
				engineAllMap.put("value_new", allNumNew);
				engineAllMap.put("unit", "个");
				resultObject.put("engineAllMap" + engineCode, engineAllMap);

				Map<String, Object> engineNewTargetMap = new HashMap<>();
				engineNewTargetMap.put("name", engineName + "新达标词总数");
				engineNewTargetMap.put("value", newTargetNum);
				engineNewTargetMap.put("name_k", "优化");
				engineNewTargetMap.put("value_k", newTargetNumK);
				engineNewTargetMap.put("name_new", "新站");
				engineNewTargetMap.put("value_new", newTargetNumNew);
				engineNewTargetMap.put("unit", "个");
				resultObject.put("engineNewTargetMap" + engineCode, engineNewTargetMap);

				Map<String, Object> engineTargetMap = new HashMap<>();
				engineTargetMap.put("name", engineName + "达标词总数");
				engineTargetMap.put("value", targetNum);
				engineTargetMap.put("name_k", "优化");
				engineTargetMap.put("value_k", targetNumK);
				engineTargetMap.put("name_new", "新站");
				engineTargetMap.put("value_new", targetNumNew);
				engineTargetMap.put("unit", "个");
				resultObject.put("engineTargetMap" + engineCode, engineTargetMap);

				Map<String, Object> engineNoTargetMap = new HashMap<>();
				engineNoTargetMap.put("name", engineName + "未达标词总数");
				engineNoTargetMap.put("value", noTargetNum);
				engineNoTargetMap.put("name_k", "优化");
				engineNoTargetMap.put("value_k", noTargetNumK);
				engineNoTargetMap.put("name_new", "新站");
				engineNoTargetMap.put("value_new", noTargetNumNew);
				engineNoTargetMap.put("unit", "个");
				resultObject.put("engineNoTargetMap" + engineCode, engineNoTargetMap);

				Map<String, Object> engineTargetRateMap = new HashMap<>();
				engineTargetRateMap.put("name", engineName + "达标率");
				engineTargetRateMap.put("value", targetRate.compareTo(BigDecimal.ZERO) > 0 ? targetRate : "-");
				engineTargetRateMap.put("name_k", "优化");
				engineTargetRateMap.put("value_k", targetRateK.compareTo(BigDecimal.ZERO) > 0 ? targetRateK : "-");
				engineTargetRateMap.put("name_new", "新站");
				engineTargetRateMap.put("value_new",
						targetRateNew.compareTo(BigDecimal.ZERO) > 0 ? targetRateNew : "-");
				engineTargetRateMap.put("unit", "%");
				resultObject.put("engineTargetRateMap" + engineCode, engineTargetRateMap);

				Map<String, Object> engineExpectConsumeMap = new HashMap<>();
				engineExpectConsumeMap.put("name", engineName + "今日预计消费");
				engineExpectConsumeMap.put("value", expectConsume);
				engineExpectConsumeMap.put("name_k", "优化");
				engineExpectConsumeMap.put("value_k", expectConsumeK);
				engineExpectConsumeMap.put("name_new", "新站");
				engineExpectConsumeMap.put("value_new", expectConsumeNew);
				engineExpectConsumeMap.put("unit", "元");
				resultObject.put("engineExpectConsumeMap" + engineCode, engineExpectConsumeMap);

				resultEList.add(resultObject);
			}
		}

		rb.put("resultList", resultList);
		rb.put("resultEList", resultEList);
		return rb.success();
	}

	@Override
	public ReturnBody getProxyReportIndex(Integer userId) {
		log.info("查询首页报表");
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> params = new HashMap<>();
		String startTime = DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -29, 1);
		String endTime = DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), 1, 1);
		params.put("userId", userId);
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		Map<String, Map<String, Object>> deduc = proxyTransactionMapper.getReportDeducDay(params);
		Map<String, Map<String, Object>> recharge = proxyRechargeMapper.getReportRechargeDay(params);
		List<String> xAxis = new ArrayList<>();
		List<Object> deducList = new ArrayList<>();
		List<Object> rechargeList = new ArrayList<>();
		for (int i = -29; i <= 0; i++) {
			String time = DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), i, 2);
			xAxis.add(time);
			if (deduc.containsKey(time)) {
				deducList.add(deduc.get(time).get("amount"));
			} else {
				deducList.add(0);
			}
			if (recharge.containsKey(time)) {
				rechargeList.add(recharge.get(time).get("amount"));
			} else {
				rechargeList.add(0);
			}
		}

		rb.put("xAxis", xAxis);
		rb.put("deducList", deducList);
		rb.put("rechargeList", rechargeList);
		return rb.success();
	}

	@Override
	public ReturnBody getUserReportIndex(Integer userId) {
		log.info("查询首页报表");
		ReturnBody rb = ReturnBody.init();
		Map<String, Map<String, Object>> resultList = new LinkedHashMap<>();
		List<Map<String, Object>> resultEList = new ArrayList<>();
		Map<String, Object> params = new HashMap<>();
		params.put("userId", userId);
		log.info("今日预计消费");
		params.put("runTime", DateUtil.getTimesmorning());
		BigDecimal todayAmount = billMapper.getReportExpectConsumeSum(params);
		BigDecimal todayNewAmount = billMapper.getReportNewExpectConsumeSum(params);
		Map<String, Object> todayMap = new HashMap<>();
		todayMap.put("name", "今日预计消费");
		todayMap.put("value", todayAmount.add(todayNewAmount));
		todayMap.put("name_k", "优化");
		todayMap.put("value_k", todayAmount);
		todayMap.put("name_new", "新站");
		todayMap.put("value_new", todayNewAmount);
		todayMap.put("unit", "元");
		resultList.put("todayMap", todayMap);

		log.info("昨日消费金额");
		params.put("startTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1));
		params.put("endTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1));
		BigDecimal yesterdayAmount = billMapper.getReportDeducSum(params);
		BigDecimal yesterdayNewAmount = billMapper.getReportNewDeducSum(params);
		Map<String, Object> yesterdayMap = new HashMap<>();
		yesterdayMap.put("name", "昨日消费");
		yesterdayMap.put("value", yesterdayAmount.add(yesterdayNewAmount));
		yesterdayMap.put("name_k", "优化");
		yesterdayMap.put("value_k", yesterdayAmount);
		yesterdayMap.put("name_new", "新站");
		yesterdayMap.put("value_new", yesterdayNewAmount);
		yesterdayMap.put("unit", "元");
		resultList.put("yesterdayMap", yesterdayMap);

		log.info("近30天消费金额");
		params.put("startTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -30, 1));
		params.put("endTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1));
		BigDecimal last30Amount = billMapper.getReportDeducSum(params);
		BigDecimal last30NewAmount = billMapper.getReportNewDeducSum(params);
		Map<String, Object> last30Map = new HashMap<>();
		last30Map.put("name", "近30天消费");
		last30Map.put("value", last30Amount.add(last30NewAmount));
		last30Map.put("name_k", "优化");
		last30Map.put("value_k", last30Amount);
		last30Map.put("name_new", "新站");
		last30Map.put("value_new", last30NewAmount);
		last30Map.put("unit", "元");
		resultList.put("last30Map", last30Map);

		log.info("上个月消费金额");
		params.put("startTime", DateUtil.getMoneFistDay());
		params.put("endTime", DateUtil.getMoneLastDay());
		BigDecimal lastMonthAmount = billMapper.getReportDeducSum(params);
		BigDecimal lastMonthNewAmount = billMapper.getReportNewDeducSum(params);
		Map<String, Object> lastMonthMap = new HashMap<>();
		lastMonthMap.put("name", "上月消费");
		lastMonthMap.put("value", lastMonthAmount.add(lastMonthNewAmount));
		lastMonthMap.put("name_k", "优化");
		lastMonthMap.put("value_k", lastMonthAmount);
		lastMonthMap.put("name_new", "新站");
		lastMonthMap.put("value_new", lastMonthNewAmount);
		lastMonthMap.put("unit", "元");
		resultList.put("lastMonthMap", lastMonthMap);

		log.info("总消费金额");
		params.clear();
		params.put("userId", userId);
		BigDecimal allAmount = billMapper.getReportDeducSum(params);
		BigDecimal allNewAmount = billMapper.getReportNewDeducSum(params);
		Map<String, Object> allMap = new HashMap<>();
		allMap.put("name", "总消费");
		allMap.put("value", allAmount.add(allNewAmount));
		allMap.put("name_k", "优化");
		allMap.put("value_k", allAmount);
		allMap.put("name_new", "新站");
		allMap.put("value_new", allNewAmount);
		allMap.put("unit", "元");
		resultList.put("allMap", allMap);

		log.info("当前余额");
		params.clear();
		Map<String, Object> user = userMapper.getUserById(userId);
		Map<String, Object> addRechargeMap = new HashMap<>();
		addRechargeMap.put("name", "余额");
		addRechargeMap.put("value", user.get("balance"));
		addRechargeMap.put("unit", "元");
		resultList.put("addRechargeMap", addRechargeMap);
		
		if (Jurisdiction.roleCode("proxy")) {
			
			params.clear();
			log.info("客户今日预计消费");
			params.put("parentId", userId);
			params.put("runTime", DateUtil.getTimesmorning());
			BigDecimal childTodayAmount = billMapper.getReportExpectConsumeSum(params);
			BigDecimal childTodayNewAmount = billMapper.getReportNewExpectConsumeSum(params);
			Map<String, Object> childTodayMap = new HashMap<>();
			childTodayMap.put("name", "客户今日预计消费");
			childTodayMap.put("value", childTodayAmount.add(childTodayNewAmount));
			childTodayMap.put("name_k", "优化");
			childTodayMap.put("value_k", childTodayAmount);
			childTodayMap.put("name_new", "新站");
			childTodayMap.put("value_new", childTodayNewAmount);
			childTodayMap.put("unit", "元");
			resultList.put("childTodayMap", childTodayMap);

			log.info("客户昨日消费金额");
			params.put("startTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1));
			params.put("endTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1));
			BigDecimal childYesterdayAmount = billMapper.getReportDeducSum(params);
			BigDecimal childYesterdayNewAmount = billMapper.getReportNewDeducSum(params);
			Map<String, Object> childYesterdayMap = new HashMap<>();
			childYesterdayMap.put("name", "客户昨日消费");
			childYesterdayMap.put("value", childYesterdayAmount.add(childYesterdayNewAmount));
			childYesterdayMap.put("name_k", "优化");
			childYesterdayMap.put("value_k", childYesterdayAmount);
			childYesterdayMap.put("name_new", "新站");
			childYesterdayMap.put("value_new", childYesterdayNewAmount);
			childYesterdayMap.put("unit", "元");
			resultList.put("childYesterdayMap", childYesterdayMap);

			log.info("客户近30天消费金额");
			params.put("startTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -30, 1));
			params.put("endTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), -1, 1));
			BigDecimal childLast30Amount = billMapper.getReportDeducSum(params);
			BigDecimal childLast30NewAmount = billMapper.getReportNewDeducSum(params);
			Map<String, Object> childLast30Map = new HashMap<>();
			childLast30Map.put("name", "客户近30天消费");
			childLast30Map.put("value", childLast30Amount.add(childLast30NewAmount));
			childLast30Map.put("name_k", "优化");
			childLast30Map.put("value_k", childLast30Amount);
			childLast30Map.put("name_new", "新站");
			childLast30Map.put("value_new", childLast30NewAmount);
			childLast30Map.put("unit", "元");
			resultList.put("childLast30Map", childLast30Map);

			log.info("客户上个月消费金额");
			params.put("startTime", DateUtil.getMoneFistDay());
			params.put("endTime", DateUtil.getMoneLastDay());
			BigDecimal childLastMonthAmount = billMapper.getReportDeducSum(params);
			BigDecimal childLastMonthNewAmount = billMapper.getReportNewDeducSum(params);
			Map<String, Object> childLastMonthMap = new HashMap<>();
			childLastMonthMap.put("name", "客户上月消费");
			childLastMonthMap.put("value", childLastMonthAmount.add(childLastMonthNewAmount));
			childLastMonthMap.put("name_k", "优化");
			childLastMonthMap.put("value_k", childLastMonthAmount);
			childLastMonthMap.put("name_new", "新站");
			childLastMonthMap.put("value_new", childLastMonthNewAmount);
			childLastMonthMap.put("unit", "元");
			resultList.put("childLastMonthMap", childLastMonthMap);

			log.info("客户总消费金额");
			params.clear();
			params.put("parentId", userId);
			BigDecimal childAllAmount = billMapper.getReportDeducSum(params);
			BigDecimal childAllNewAmount = billMapper.getReportNewDeducSum(params);
			Map<String, Object> childAllMap = new HashMap<>();
			childAllMap.put("name", "总消费");
			childAllMap.put("value", childAllAmount.add(childAllNewAmount));
			childAllMap.put("name_k", "优化");
			childAllMap.put("value_k", childAllAmount);
			childAllMap.put("name_new", "新站");
			childAllMap.put("value_new", childAllNewAmount);
			childAllMap.put("unit", "元");
			resultList.put("childAllMap", childAllMap);
			
			log.info("今日新增客户");
			params.clear();
			params.put("creationDate", DateUtil.getTimesmorning());
			params.put("parentId", userId);
			int addUserCnt = userMapper.getUserCnt(params);
			Map<String, Object> addUserCntMap = new HashMap<>();
			addUserCntMap.put("name", "今日新增客户");
			addUserCntMap.put("value", addUserCnt);
			addUserCntMap.put("unit", "人");
			resultList.put("addUserCntMap", addUserCntMap);
			
			log.info("今日客户充值");
			params.clear();
			params.put("parentId", userId);
			params.put("startTime", DateUtil.getTimesmorning());
			params.put("endTime", DateUtil.getAfterDayDate(DateUtil.getTimesmorning(), 1, 1));
			BigDecimal childAddRecharge = rechargeMapper.getReportRechargeSum(params);
			Map<String, Object> childAddRechargeMap = new HashMap<>();
			childAddRechargeMap.put("name", "今日客户充值");
			childAddRechargeMap.put("value", childAddRecharge);
			childAddRechargeMap.put("unit", "元");
			resultList.put("addChildRechargeMap", childAddRechargeMap);
		}
		params.clear();
		params.put("userId", userId);
		params.put("runTime", DateUtil.getTimesmorning());

		List<Map<String, Object>> engineList = billMapper.getEngineList();

		if (engineList != null && engineList.size() > 0) {
			Map<String, Map<String, Object>> engineStatistics = billMapper.getEngineStatistics(params);
			Map<String, Map<String, Object>> engineStatisticsNew = billMapper.getNewEngineStatistics(params);
			for (Map<String, Object> engine : engineList) {
				Map<String, Object> resultObject = new LinkedHashMap<>();
				String engineCode = (String) engine.get("code");
				Map<String, Object> obj = engineStatistics.get(engineCode);
				Map<String, Object> objNew = engineStatisticsNew.get(engineCode);
				if (obj == null && objNew == null) {
					continue;
				}
				String engineName = (String) engine.get("name");
				Integer allNum = 0, allNumK = 0, allNumNew = 0;
				Integer targetNum = 0, targetNumK = 0, targetNumNew = 0;
				Integer newTargetNum = 0, newTargetNumK = 0, newTargetNumNew = 0;
				Integer noTargetNum = 0, noTargetNumK = 0, noTargetNumNew = 0;
				BigDecimal expectConsume = BigDecimal.ZERO, expectConsumeK = BigDecimal.ZERO,
						expectConsumeNew = BigDecimal.ZERO;
				BigDecimal targetRate = BigDecimal.ZERO, targetRateK = BigDecimal.ZERO, targetRateNew = BigDecimal.ZERO;
				if (obj != null) {
					allNumK = (Integer) obj.get("allNum");
					targetNumK = (Integer) obj.get("targetNum");
					newTargetNumK = (Integer) obj.get("newTargetNum");
					noTargetNumK = (Integer) obj.get("noTargetNum");
					expectConsumeK = (BigDecimal) obj.get("expectConsume");
					allNum += allNumK;
					targetNum += targetNumK;
					newTargetNum += newTargetNumK;
					noTargetNum += noTargetNumK;
					expectConsume = expectConsume.add(expectConsumeK);
				}
				if (objNew != null) {
					allNumNew = (Integer) objNew.get("allNum");
					targetNumNew = (Integer) objNew.get("targetNum");
					newTargetNumNew = (Integer) objNew.get("newTargetNum");
					noTargetNumNew = (Integer) objNew.get("noTargetNum");
					expectConsumeNew = (BigDecimal) objNew.get("expectConsume");
					allNum += allNumNew;
					targetNum += targetNumNew;
					newTargetNum += newTargetNumNew;
					noTargetNum += noTargetNumNew;
					expectConsume = expectConsume.add(expectConsumeNew);
				}
				if (allNum > 0) {
					targetRate = new BigDecimal(targetNum).divide(new BigDecimal(allNum), 4, BigDecimal.ROUND_HALF_UP)
							.multiply(new BigDecimal(100)).setScale(2);
				}
				if (allNumK > 0) {
					targetRateK = new BigDecimal(targetNumK)
							.divide(new BigDecimal(allNumK), 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100))
							.setScale(2);
				}
				if (allNumNew > 0) {
					targetRateNew = new BigDecimal(targetNumNew)
							.divide(new BigDecimal(allNumNew), 4, BigDecimal.ROUND_HALF_UP)
							.multiply(new BigDecimal(100)).setScale(2);
				}

				Map<String, Object> engineAllMap = new HashMap<>();
				engineAllMap.put("name", engineName + "词总数");
				engineAllMap.put("value", allNum);
				engineAllMap.put("name_k", "优化");
				engineAllMap.put("value_k", allNumK);
				engineAllMap.put("name_new", "新站");
				engineAllMap.put("value_new", allNumNew);
				engineAllMap.put("unit", "个");
				resultObject.put("engineAllMap" + engineCode, engineAllMap);

				Map<String, Object> engineNewTargetMap = new HashMap<>();
				engineNewTargetMap.put("name", engineName + "新达标词总数");
				engineNewTargetMap.put("value", newTargetNum);
				engineNewTargetMap.put("name_k", "优化");
				engineNewTargetMap.put("value_k", newTargetNumK);
				engineNewTargetMap.put("name_new", "新站");
				engineNewTargetMap.put("value_new", newTargetNumNew);
				engineNewTargetMap.put("unit", "个");
				resultObject.put("engineNewTargetMap" + engineCode, engineNewTargetMap);

				Map<String, Object> engineTargetMap = new HashMap<>();
				engineTargetMap.put("name", engineName + "达标词总数");
				engineTargetMap.put("value", targetNum);
				engineTargetMap.put("name_k", "优化");
				engineTargetMap.put("value_k", targetNumK);
				engineTargetMap.put("name_new", "新站");
				engineTargetMap.put("value_new", targetNumNew);
				engineTargetMap.put("unit", "个");
				resultObject.put("engineTargetMap" + engineCode, engineTargetMap);

				Map<String, Object> engineNoTargetMap = new HashMap<>();
				engineNoTargetMap.put("name", engineName + "未达标词总数");
				engineNoTargetMap.put("value", noTargetNum);
				engineNoTargetMap.put("name_k", "优化");
				engineNoTargetMap.put("value_k", noTargetNumK);
				engineNoTargetMap.put("name_new", "新站");
				engineNoTargetMap.put("value_new", noTargetNumNew);
				engineNoTargetMap.put("unit", "个");
				resultObject.put("engineNoTargetMap" + engineCode, engineNoTargetMap);

				Map<String, Object> engineTargetRateMap = new HashMap<>();
				engineTargetRateMap.put("name", engineName + "达标率");
				engineTargetRateMap.put("value", targetRate.compareTo(BigDecimal.ZERO) > 0 ? targetRate : "-");
				engineTargetRateMap.put("name_k", "优化");
				engineTargetRateMap.put("value_k", targetRateK.compareTo(BigDecimal.ZERO) > 0 ? targetRateK : "-");
				engineTargetRateMap.put("name_new", "新站");
				engineTargetRateMap.put("value_new",
						targetRateNew.compareTo(BigDecimal.ZERO) > 0 ? targetRateNew : "-");
				engineTargetRateMap.put("unit", "%");
				resultObject.put("engineTargetRateMap" + engineCode, engineTargetRateMap);

				Map<String, Object> engineExpectConsumeMap = new HashMap<>();
				engineExpectConsumeMap.put("name", engineName + "今日预计消费");
				engineExpectConsumeMap.put("value", expectConsume);
				engineExpectConsumeMap.put("name_k", "优化");
				engineExpectConsumeMap.put("value_k", expectConsumeK);
				engineExpectConsumeMap.put("name_new", "新站");
				engineExpectConsumeMap.put("value_new", expectConsumeNew);
				engineExpectConsumeMap.put("unit", "元");
				resultObject.put("engineExpectConsumeMap" + engineCode, engineExpectConsumeMap);
				resultEList.add(resultObject);
			}
		}

		rb.put("resultList", resultList);
		rb.put("resultEList", resultEList);
		return rb.success();
	}

}
